package net.dao.impl;

import net.dao.UserDao;
import net.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * Created by zyh on 2017/2/27.
 */

@Repository
public class UserDaoImpl implements UserDao{
    @Resource
    private SessionFactory sessionFactory;
    @Override
    public boolean findByUsernamePassword(String username, String password) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        User user =null;
               user = (User) session.createQuery(
                "from User a where a.username = ? ")
                .setParameter(0,username)
                .uniqueResult();
        System.out.println(user);
        if (user==null){
            User user1 =new User(username,password);
            session.save(user1);
            return true;
        }
        transaction.commit();
        session.close();
        return false;
    }
}
